<?php
/**
 * app端 产品搜索管理
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/11/13 14:47
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Controllers\Api\V1\App;


use App\Http\Controllers\ApiController;
use App\Http\Requests\Products\ProductCreateRequest;
use App\Http\Requests\Products\ProductIndexRequest;
use App\Http\Requests\Products\ProductSearchAutoCompleteRequest;
use App\Models\IndexConfig\SearchRules\ProductDescriptionAutoCompleteSearchRule;
use App\Models\Products\Product;
use App\Models\Products\ProductDescription;
use App\Models\Products\ProductToCategory;
use App\Models\Store\Store;
use App\Services\MobileApp\AppEnv;
use App\Services\Repositories\ProductRepository;
use Illuminate\Http\Request;


class ProductController extends ApiController
{
    const IS_BEST_SELLER = 1; // 查询热销商品

    private $productRepository;

    public function __construct(ProductRepository $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    /**
     * 根据产品的id数组获取产品的信息列表
     * @param Request $request
     * @return array|\Illuminate\Http\Response
     */
    public function indexByIdArray(Request $request)
    {
        $language_id = AppEnv::languageId();
        $currency = AppEnv::currency();
        // 获取ID列表中的商品列表
        $products = $this->productRepository
                        ->getProductsByIdArray(json_decode($request->get('product_ids')));

        return $this->response(compact('products'));
    }

    /**
     * 产品查询选项
     *
     * @param ProductCreateRequest $request
     *
     * @return array|\Illuminate\Http\Response
     */
    public function create(ProductCreateRequest $request)
    {
        $store_id = AppEnv::storeId();

        // 获取分类下的商品ids
        $product_ids = (new ProductToCategory)
//            ->has('productByOnSale')  // index（）接口已经验证了！
            ->filterCategory($request->category_id)
            ->filterStore($store_id)
            ->pluck('product_id')->unique()->values()->all();

        // 库存状态 (前端无库存时，商品详情中才显示)
        $stock_status = $this->productRepository->getFilterStockStatus($product_ids);

        // 品牌
        $manufacturers = $this->productRepository->getFilterManufacturers($product_ids, $store_id);

        // 选项
        $options = $this->productRepository->getFilterOptions($product_ids);

        // 属性
        $attributes = $this->productRepository->getFilterAttributes($product_ids);

        return $this->response(compact('stock_status', 'manufacturers', 'options', 'attributes'));
    }


    /**
     * 搜索产品列表
     *
     * @param ProductIndexRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(ProductIndexRequest $request)
    {
        $products = $this->productRepository->getProducts($request);

        return $this->response(compact('products'));
    }

    /**
     * App端首页，更多商品的搜索
     *
     * @param ProductIndexRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function indexAppProducts(ProductIndexRequest $request)
    {
        $products = $this->productRepository->getAppProducts($request);

        return $this->response(compact('products'));
    }

    /**
     * 模糊搜索提示
     *
     * @param ProductSearchAutoCompleteRequest $request
     *
     * @return array|\Illuminate\Http\Response
     */
    public function searchAutoComplete(ProductSearchAutoCompleteRequest $request)
    {
        $prod_description = (new ProductDescription)
            ->search($request->keyword)
            ->rule(ProductDescriptionAutoCompleteSearchRule::class)
            ->where('language_id', AppEnv::languageId())
            ->take($request->limit ?: 15)
            ->get();

        $hints = $prod_description->pluck('name')->all();

        return $this->response(compact('hints'));
    }
}
